package com.yuzhitong.shapi.widget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;

import com.yuzhitong.shapi.R;


/**
 * loading的背景View
 *
 * @author MLT
 */
public class PlayLoadView extends FrameLayout {
    private ObjectAnimator operatingAnim;
    private View background;

    public PlayLoadView(Context context) {
        super(context);
        initView(context);
    }

    public PlayLoadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public PlayLoadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context) {
        // 设置背景图片View
        background = new View(context);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        background.setBackgroundResource(R.mipmap.load);
        background.setLayoutParams(params);
        // 添加背景到frameLayout
        addView(background);

        // 设置前景图片View
        //        View center = new View(context);
        //        center.setBackgroundResource(R.mipmap.loading_cneter);
        //        center.setLayoutParams(params);
        // 添加前景到frameLayout
        //        addView(center);

        //        setBackgroundColor(0xCC000000);

        operatingAnim = ObjectAnimator.ofFloat(background, "rotation", 0, 360);
        // 设置动画每次播放的时长
        operatingAnim.setDuration(1000);
        // 设置动画播放速度
        operatingAnim.setInterpolator(new LinearInterpolator());
        // 设置动画循环播放
        operatingAnim.setRepeatCount(ValueAnimator.INFINITE);
    }

    /**
     * 开启动画
     */
    public void startLoop() {
        if (operatingAnim != null) {
            operatingAnim.start();
        }
    }

    /**
     * 关闭动画
     */
    public void stopLoop() {
        if (operatingAnim != null) {
            operatingAnim.cancel();
        }
    }
}
